package cn.itsource.org.controller;

import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.utli.AjaxResult;
import cn.itsource.utli.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工控制层
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    /**
     * 查询全部
     * @return
     */
    @GetMapping
    public List<Employee> getAll(){
        return employeeService.queryAll();
    }

    /**
     * 新增和修改
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "员工添加或修改", notes = "如果有id就是修改否则就是添加")
    public AjaxResult saveOeUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            employeeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    /**
     * 查询单个数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id) {
        return employeeService.queryById(id);
    }
    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    public PageInfo<Employee> page(@RequestBody EmployeeQuery query) {
        return employeeService.queryPage(query);
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody Long[] ids) {
        try {
            employeeService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
}
